{extend name="common/base" /}
{block name="body"} 
<div class="Content" id="content">
    <div class="container">
        <div class="cashNav">
            <a class="item active" href="finance_cash.html">提现</a>
            <a class="item " href="finance_log.html">提现记录</a>
            <a class="item " href="finance_account.html">提现账户</a>
        </div>

        <div class="CashPage">
            <ul class="info">
                <li>
                    总可提现：<b>¥{$merchant.balance|default=0}</b>
                </li>
                <li>
                    待结算金额：<b>¥{$merchant.amount|default=0}</b>
                </li>
            </ul>
            <div class="text">
                <p class="tag">提现规则：(可提现金额须≥¥100，每天最多提现3次)</p>
                {if !$merchant_account_exits} 
                    <p class="tag">您还没有设置提现账号，<a class="red" href="/merchant/finance_account.html">去设置>> </a></p>
                {/if}
            </div>

            <div class="content">
                <div class="FormPage">
                    <form class="layui-form" lay-filter="withdrawForm">
                        <div class="form-item-inline">
                            <label class="must form-label">提现金额：</label>
                            <div class="input-inline">
                                <input type="number" name="amount" min="100" class="layui-input"
                                    placeholder="请输入提现金额" lay-verify="required|number|amountCheck">
                                <p class="layui-form-mid layui-word-aux">最小提现金额100元</p>
                            </div>
                        </div>
                        
                        <div class="form-item-inline">
                            <label class="form-label">收款人：</label>
                            <div class="input-inline">
                                {$merchant_account.account_user|default='未设置'}
                                <input type="hidden" name="account_user" value="{$merchant_account.account_user|default=''}">
                            </div>
                        </div>
                        
                        <!-- <div class="form-item-inline">
                            <label class="form-label">银行卡号：</label>
                            <div class="input-inline">
                                {$merchant_account.account|default='未设置'}
                                <input type="hidden" name="account" value="{$merchant_account.account|default=''}">
                            </div>
                        </div> -->
                        
                        <div class="form-item-inline">
                            <label class="must form-label">交易密码：</label>
                            <div class="input-inline">
                                <input type="password" name="trade_password" class="layui-input"
                                    placeholder="请输入交易密码" lay-verify="required">
                                <a class="red" id="forgetPassword" style="margin-left: 10px;">忘记交易密码？</a>
                            </div>
                        </div>

                        <div class="form-item-inline">
                            <label class="form-label"></label>
                            <div class="input-inline foot">
                                <button class="layui-btn" lay-submit lay-filter="submitWithdraw">确认提现</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            <div class="tips">
                <b>说明</b>
                <p>1.企业服务方提现需先向平台提供发票后解锁金额(发票开出后，请提交发票已开出提醒，通知平台解锁)，发票抬头请见服务费页面底部<br>
                    2.充值金额不能提现，可提现金额来自您的收入。</p>
            </div>
        </div>
    </div>
</div>
{/block}

{block name="js"} 
<script>
    changeTitleName('提现');
    
    layui.use(['form', 'layer', 'jquery'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;
        
        // 自定义验证规则
        form.verify({
            amountCheck: function(value){
                var balance = parseFloat("{$merchant.balance|default=0}");
                value = parseFloat(value);
                
                if(value < 100){
                    return '提现金额不能少于100元';
                }
                
                if(value > balance){
                    return '提现金额不能超过可提现余额';
                }
                
                // 可以添加每日提现次数限制的检查
            }
        });
        
        // 忘记密码点击事件
        $('#forgetPassword').on('click', function(){
            layer.open({
                type: 2,
                title: '找回交易密码',
                area: ['500px', '400px'],
                content: '/merchant/reset_transaction_password.html'
            });
        });
        
        // 表单提交
        form.on('submit(submitWithdraw)', function(data){
            // 检查是否设置了提现账户
            {if !$merchant_account_exits}
                layer.msg('请先设置提现账户', {icon: 2});
                return false;
            {/if}
            
            var loading = layer.load(2);
            
            $.ajax({
                url: '/merchant/withdraw_apply',
                type: 'POST',
                dataType: 'json',
                data: data.field,
                success: function(res){
                    layer.close(loading);
                    if(res.code === 200){
                        layer.msg('提现申请已提交', {icon: 1});
                        // 可以刷新页面或跳转到提现记录
                        setTimeout(function(){
                            location.href = '/merchant/finance_log.html';
                        }, 1500);
                    }else{
                        layer.msg(res.msg || '提现申请失败', {icon: 2});
                    }
                },
                error: function(){
                    layer.close(loading);
                    layer.msg('网络错误，请稍后再试', {icon: 2});
                }
            });
            
            return false;
        });
    });
</script>
{/block}